<?php

include 'DBconn.php';

class UserMasterDetails {

	public function addNewUser($VObject,$image) 
	{
		$image_fk = $this->addUserImage($image);
		
        $query = "insert into user_header(emp_id,user_name,login_name,user_password,user_phone,user_designation,user_department,user_role,user_email,user_image_fk,isactive,cre_userid,cre_datetime,activestatus)values('$VObject->emp_id','$VObject->user_name','$VObject->login_name','$VObject->user_password','$VObject->contact_number','$VObject->user_designation','$VObject->user_department','$VObject->user_role','$VObject->user_email','$image_fk',1,'$VObject->cre_userid','$VObject->cre_datetime',1)";
        $s = mysql_query("$query") or die(mysql_error());
    }
	
    public function deleteUserImage($serialnumber) 
	{
        $query = "update user_image_master set  activestatus=0 where serialnumber='$serialnumber'";
        $s = mysql_query("$query") or die(mysql_error());
    }	

	public function editUserDetails($VObject,$image) 
	{
		$image_fk = $this->updateUserImage($image,$VObject->user_image_fk);
		
        $query = "update user_header set emp_id='$VObject->emp_id',user_name='$VObject->user_name',login_name='$VObject->login_name',user_password='$VObject->user_password',user_phone='$VObject->contact_number',user_designation='$VObject->user_designation',user_department='$VObject->user_department',user_role='$VObject->user_role',user_email='$VObject->user_email','$VObject->',user_image_fk='$VObject->user_image_fk' where serialnumber='$VObject->serialnumber'";
        $s = mysql_query($query) or die(mysql_error());
    }
	
    public function addUserImage($image) 
	{
	    $query = "insert into user_image_master(user_image,activestatus)values('$image',1)";
        $s = mysql_query("$query") or die(mysql_error());
        $last_id = mysql_insert_id();
        return (int) $last_id;
    }

    public function updateUserImage($image,$user_image_fk)
	{
        $query = "update user_image_master set user_image='$image' where serialnumber='$user_image_fk' and activestatus=1";
        $s = mysql_query("$query") or die(mysql_error());
    }
	
    public function deleteUserDetails($Serialnumber) {
        $s = mysql_query("update user_header set activestatus=0 where serialnumber='$Serialnumber'") or die(mysql_error());
        return 'true';
    }

    public function getUserImage($serialnumber) {
        $query = "select * from user_image_master where serialnumber='$serialnumber' and activestatus=1";
        $result = mysql_query($query);
        $row = mysql_fetch_object($result);

        $temp = new VOUserImage();
        $temp->serialnumber = (int) $row->serialnumber;
        $temp->user_image = $row->user_image;
        $temp->activestatus = (int) $row->activestatus;

        mysql_free_result($result);
        return $temp;
    }

    public function getAllUsers() {
        $result = mysql_query("SELECT * from user_header where isactive=1 and activestatus=1") or die(mysql_errno());
        $ret = array();
        while ($row = mysql_fetch_object($result)) {
            $temp = new VOUserMaster();
            $temp->serialnumber = (int) $row->serialnumber;
            $temp->emp_id = $row->emp_id;
            $temp->user_name = $row->user_name;
            $temp->login_name = $row->login_name;
            $temp->user_password = $row->user_password;
            $temp->contact_number = $row->user_phone;
            $temp->user_designation = $row->user_designation;
            $temp->user_department = $row->user_department;
            $temp->user_role = $row->user_role;
            $temp->user_email = $row->user_email;
			$temp->user_image_fk = (int) $row->user_image_fk;
            $temp->isactive = (boolean) $row->isactive;
            $temp->cre_userid = (int) $row->cre_userid;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;
            $ret[] = $temp;
        }
        mysql_free_result($result);
        return $ret;
    } 

}

class VOUserImage {

    var $serialnumber;
    var $user_image;
    var $activestatus;

}

class VOUserMaster {

    var $serialnumber;
    var $emp_id;
    var $user_name;
    var $login_name;
    var $user_password;
    var $contact_number;
    var $user_designation;
    var $user_department;
    var $user_role;
    var $user_email;
	var $user_image_fk;
    var $isactive;
    var $cre_userid;
    var $cre_datetime;
    var $activestatus;

}

?>